﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Otgruzka.Controls.jQuery
{
    public partial class JQueryDatepicker : JQueryControlsBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //RegisterScriptReference("~/JScripts/jquery.maskedinput-1.3.min.js");
        }

        public DateTime? SelectedDate
        {
            get
            {
                return Value.Text.GetDateFromString();
            }
            set
            {
                Value.Text = value.GetDateString();
            }
        }

        public event EventHandler<EventArgs> SelectedDateChanged;

        protected override void OnPreRender(EventArgs e)
        {
            object clientSettings = new
            {
                Value = SelectedDate.GetDateString(),
                ButtonImageUrl = Page.ResolveClientUrl("~/Images/calendar.png")
            };

            RegisterPlugin(Container.ClientID, "JQueryDatepicker", clientSettings);
        }

        protected void Value_TextChanged(object sender, EventArgs e)
        {
            //Multithread sync
            var tmpEvent = SelectedDateChanged;

            if (tmpEvent != null)
                tmpEvent(this, new EventArgs());
        }
    }
}